/**
 * @Author: dn-jinmin
 * @File:  example
 * @Version: 1.0.0
 * @Date: 2024/3/7
 * @Description:
 */

package main

import (
	"context"
	"errors"
	"gitee.com/dn-jinmin/tlog"
	"time"
)

func main() {
	// 初始化
	tlog.Init(
		tlog.WithMode(tlog.All),
		tlog.WithLabel("tlog示例"),
		tlog.WithLoggerWriter(tlog.NewLoggerWriter()),
	)

	startTime := time.Now()

	ctx := tlog.TraceStart(context.Background())

	tlog.InfoCtx(ctx, "", "tlog示例")

	service := NewOrderService()
	service.GetOrder(ctx, "1234567890")
	time.Sleep(time.Microsecond * 3)
	tlog.InfoCtx(ctx, "", "tlog示例 结束", tlog.RTField(startTime, time.Now()))
	time.Sleep(time.Second)
}

// ------------- service ---------------------

type OrderService struct {
	*OrderModel
}

func NewOrderService() *OrderService {
	return &OrderService{
		OrderModel: NewOrderModel(),
	}
}
func (s *OrderService) GetOrder(ctx context.Context, orderId string) (interface{}, error) {

	tlog.DebugfCtx(ctx, orderId, "orderId %v", orderId)

	data, err := s.OrderModel.GetOrder(ctx, orderId)
	if err != nil {
		tlog.ErrorfCtx(ctx, orderId, "orderId %v, err %v", orderId, err)
	}

	tlog.InfofCtx(ctx, orderId, "orderId %v, data %v", orderId, data)

	return "订单数据", errors.New("示例的异常")
}

// -------------- model ---------------------

type OrderModel struct{}

func NewOrderModel() *OrderModel {
	return &OrderModel{}
}
func (m *OrderModel) GetOrder(ctx context.Context, orderId string) (interface{}, error) {
	tlog.InfofCtx(ctx, orderId, "orderId %v", orderId)
	return "订单数据", errors.New("示例的异常")
}
